package com.zhu.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2023/05/23/17:00
 * @Description:
 */
public class ProxyUtil {

    public static Star creatProxyObject(BigStar bigStar) {


        Star star = (Star) Proxy.newProxyInstance(
                ProxyUtil.class.getClassLoader(),
                new Class[]{Star.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if ("sing".equals(method.getName())) {
                            System.out.println("先准备话筒，再唱歌...");
                        }else if("dance".equals(method.getName())) {
                            System.out.println("先准备场地收钱后，再跳舞...");
                        }

                        return method.invoke(bigStar, args);
                    }
                }
        );

        return star;

    }
}
